package edu.isquen.core.document.model;

import java.io.Serializable;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Item implements Serializable{

	private static final long serialVersionUID = -8916213264874474157L;
	
	public static final String VALUE_TYPE_VARCHAR = "VALUE_TYPE_VARCHAR";

	public static final String VALUE_TYPE_NUMBER = "VALUE_TYPE_NUMBER";

	public static final String VALUE_TYPE_DATE = "VALUE_TYPE_DATE";

	public static final String VALUE_TYPE_BLOB = "VALUE_TYPE_BLOB";

	public static final String VALUE_TYPE_TEXT = "VALUE_TYPE_TEXT";
	
	public static final String VALUE_TYPE_STRINGLIST = "VALUE_TYPE_STRINGLIST";

	public static final String VALUE_TYPE_INCLUDE = "VALUE_TYPE_INCLUDE";

	public static final DecimalFormat DECIMAL_FORMAT = new DecimalFormat(
			"00000000000000000000.000000");

	public static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat(
			"yyyy-MM-dd hh:mm:ss");

	public static final SimpleDateFormat DATE_FORMAT_SHORT = new SimpleDateFormat(
			"yyyy-MM-dd");
	
	private Document document;
	
	private String id;
	
	private String name;
	
	private Date lastModifyDate;
	
	private String type;
	
	private String formname;
	
	private String varcharValue;

	private Double numberValue;

	private String textValue;

	private Date dateValue;
	
	public void setValue(Object value) {
		if (value == null) {
			// Nothing to do
		} else if (this.getType().equals(Item.VALUE_TYPE_VARCHAR)) {
			this.setVarcharValue(value + "");
		} else if (this.getType().equals(Item.VALUE_TYPE_TEXT)) {
			this.setTextValue(value + "");
		} else if (value != null && this.getType().equals(Item.VALUE_TYPE_DATE)) {
			if (value instanceof Date) {
				this.setDateValue((Date) value);
			} else if (value instanceof String) {
				try {
					this.setDateValue(DATE_FORMAT.parse((String) value));
				} catch (Exception e) {
					try {
						this.setDateValue(DATE_FORMAT_SHORT
								.parse((String) value));
					} catch (Exception e1) {
						this.setDateValue(null);
					}
				}
			}
		} else if (value != null
				&& this.getType().equals(Item.VALUE_TYPE_NUMBER)) {
			Double d = new Double(0.0);
			if (value instanceof Number) {
				d = new Double(((Number) value).doubleValue());
			} else if (value instanceof String) {
				
			}
			this.setNumberValue(d);
		}
	}
	
	public Document getDocument() {
		return document;
	}

	public void setDocument(Document document) {
		this.document = document;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Date getLastModifyDate() {
		return lastModifyDate;
	}

	public void setLastModifyDate(Date lastModifyDate) {
		this.lastModifyDate = lastModifyDate;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getFormname() {
		return formname;
	}

	public void setFormname(String formname) {
		this.formname = formname;
	}
	
	public String getVarcharValue() {
		return varcharValue;
	}

	public void setVarcharValue(String varcharValue) {
		this.varcharValue = varcharValue;
	}

	public Double getNumberValue() {
		return numberValue;
	}

	public void setNumberValue(Double numberValue) {
		this.numberValue = numberValue;
	}

	public String getTextValue() {
		return textValue;
	}

	public void setTextValue(String textValue) {
		this.textValue = textValue;
	}

	public Date getDateValue() {
		return dateValue;
	}

	public void setDateValue(Date dateValue) {
		this.dateValue = dateValue;
	}
	
}
